package com.loong.member.business;

import com.alibaba.fastjson.JSONArray;
import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.member.common.forehead.param.CartItemForeheadParam;
import com.loong.member.model.CartItem;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description:购物详情持久层
 * @Author: 刘群
 * @Date: 2020/10/23 17:17
 */
@Repository
public class CartItemBusiness extends AbstractBusiness<CartItem> {
    /**
     * 查询购物详情
     *
     * @param param 查询参数：购物车id、商品id、商品规格id
     * @return 购物详情集合
     */
    public List<CartItem> findCartItemByParam(CartItemForeheadParam param) {
        Condition condition = new Condition(CartItem.class);
        Example.Criteria criteria = condition.createCriteria();
        //根据购物车id、商品id、商品规格id查询是否存在该购物详情
        criteria.andEqualTo("cartId", param.getCartId());
        criteria.andEqualTo("goodsId", param.getGoodsSkuId());
        criteria.andEqualTo("goodsSizeId", JSONArray.toJSONString(param.getGoodsSpecification()));
        return findByCondition(condition);
    }

    /**
     * 根据购物车id查询购物详情
     *
     * @param cartId 购物车id
     * @return 购物详情集合
     */
    public List<CartItem> findCartItemByCartId(Long cartId) {
        Condition condition = new Condition(CartItem.class);
        Example.Criteria criteria = condition.createCriteria();
        //根据购物车id查询购物详情
        criteria.andEqualTo("cartId", cartId);
        return findByCondition(condition);
    }

    /**
     * 根据购物车id删除购物详情
     *
     * @param cartId 购物车id
     */
    public void deleteCartItemByCartId(Long cartId) {
        Condition condition = new Condition(CartItem.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("cartId", cartId);
        deleteByCondition(condition);
    }
}
